package com.wuyou.mid;


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 870. 优势洗牌
 */
public class AdvantageCount {


    public static void main(String[] args) {
        AdvantageCount advantageCount = new AdvantageCount();
//        int[] nums1 = {2,7,11,15};
//        int[] nums2 = {1,10,4,11};
        int[] nums1 = {12, 24, 8, 32};
        int[] nums2 = {13, 25, 32, 11};
        advantageCount.solution_1(nums1, nums2);
    }

    public int[] solution_1(int[] nums1, int[] nums2) {
        int n = nums1.length;
        int[][] index = new int[n][2];
        for(int i = 0; i < n; i++) {
            index[i][0] = nums2[i];
            index[i][1] = i;
        }
        Arrays.sort(nums1);
        Arrays.sort(index, (a, b) -> b[0] - a[0]);
        int left = 0, right = n - 1;
        for(int i = 0; i < n; i++) {
            if(nums1[right] > index[i][0]) {
                nums2[index[i][1]] = nums1[right];
                right--;
            } else {
                nums2[index[i][1]] = nums1[left];
                left++;
            }
        }
        return nums2;
    }
}
